package com.wxh.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: DateUtils
 * @Description: 时间工具类
 * @Author wxh
 * @Date: 2020/9/4 9:49
 * @Version V1.0.0
 * @Since 1.8
 */
public final class DateUtils {

    private DateUtils(){

    }



    /**
     * 求某个时间前N天的时间字符串
     * @param dateString 传入时间入参
     * @param days 前几天
     * @param pattern 格式
     * @return String
     */
    public static String getDateBefore(String dateString, int days, String pattern){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date = sdf.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -days);
            Date dBefore = calendar.getTime();
            String dateStr = sdf.format(dBefore);
            return dateStr;
        } catch (ParseException e) {
            throw new java.lang.IllegalArgumentException("parameter date is not valid");
        }

    }


    /**
     * 根据传入的时间求上周日的时间
     * @param dataStr 传入时间入参
     * @return String
     */
    public static String getPrevSundayDate(String dataStr){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
            Date  time = sdf.parse(dataStr);
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(time);
            int dayOfWeek = calendar2.get(Calendar.DAY_OF_WEEK) - 1;
            int offset2 = 7 - dayOfWeek;
            calendar2.add(Calendar.DATE, offset2 - 7);
            return sdf.format(calendar2.getTime());
        } catch (ParseException e) {
            throw new java.lang.IllegalArgumentException("parameter date is not valid");
        }
    }

    /**
     * 根据传入的时间求上周一的时间
     * @param dataStr 传入时间入参
     * @return String
     */
    public static String getPrevMondayDate(String dataStr){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
            Calendar calendar1 = Calendar.getInstance();
            Date  time = sdf.parse(dataStr);
            calendar1.setTime(time);
            int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
            int offset1 = 1 - dayOfWeek;
            calendar1.add(Calendar.DATE, offset1 - 7);
            return sdf.format(calendar1.getTime());
        } catch (ParseException e) {
            throw new java.lang.IllegalArgumentException("parameter date is not valid");
        }
    }

    /**
     * 根据周一的时间取周日的时间
     * @param dataStr 传入的时间字符串
     * @return String
     */
    public static String getSundayDate(String dataStr){
        try {
            // 设置时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
            Calendar cal = Calendar.getInstance();
            Date time =  sdf.parse(dataStr);
            cal.setTime(time);
            cal.add(Calendar.DATE, 6);
            System.out.println("所在周星期日的日期："+sdf.format(cal.getTime()));
            return sdf.format(cal.getTime());
        } catch (ParseException e) {
            throw new java.lang.IllegalArgumentException("parameter date is not valid");
        }
    }

    /**
     * 根据传入的时间获取周一的时间
     * @param dataStr 传入的时间字符串
     * @return String
     */
    public static String getMondayDate(String dataStr){
        try {
            // 设置时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
            Calendar cal = Calendar.getInstance();
            Date time =  sdf.parse(dataStr);
            cal.setTime(time);
            // 输出要计算日期
            System.out.println("要计算日期为:"+sdf.format(cal.getTime()));
            // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
            int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
            // 获得当前日期是一个星期的第几天
            if(1 == dayWeek) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
            // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
            cal.setFirstDayOfWeek(Calendar.MONDAY);
            // 获得当前日期是一个星期的第几天
            int day = cal.get(Calendar.DAY_OF_WEEK);
            // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
            cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
            System.out.println("所在周星期一的日期："+sdf.format(cal.getTime()));
            return sdf.format(cal.getTime());
        } catch (ParseException e) {
            throw new java.lang.IllegalArgumentException("parameter date is not valid");
        }
    }


    /**
     * 比较两个时间，谁在前，谁在后
     * @return boolean
     */
    public static boolean twoDateCompare(){
        Calendar calendar = Calendar.getInstance();
        // 当前时间
        Date date1 = calendar.getTime();
        // date1: 2020-09-04 10:00:02
        System.out.println("date1: "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1));
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        // 当时时间的 前七天的时间
        Date date2 = calendar.getTime();
        // date2: 2020-08-28 10:00:02
        System.out.println("date2: "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date2));
        if(date2.before(date1)){
            return true;
        }else{
            return false;
        }
    }



    public static void main(String[] args) {
//        twoDateCompare();
//        System.out.println(getMondayDate("2021012113"));
//        System.out.println("hour: "+getDateBefore("2021012213", 1, "yyyyMMddHH"));
//        System.out.println("day: "+getDateBefore("20210122", 1, "yyyyMMdd"));
//        System.out.println("2021012213".substring(0,6)+"00"+"2021012213".substring(8,10));
//        System.out.println("2021012213".substring(8,10));
//        System.out.println("2021012213".substring(0,8));

//        String mondayDate = getMondayDate("2021012113");
//        System.out.println(mondayDate);
//        System.out.println(getSundayDate(mondayDate));

        System.out.println(getPrevMondayDate("2021012714"));
        System.out.println(getPrevSundayDate("2021012714"));
    }
}
